<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            //极简的实现
            class Promise {
                callbacks = [];
                constructor(fn) {
                    console.log('constructor()...', fn, this);
                    fn(this._resolve.bind(this));
                }
                then(onFulfilled) {
                    console.log('then()...', onFulfilled);
                    this.callbacks.push(onFulfilled);
                }
                _resolve(value) {
                    console.log('_resolve()...', value);
                    this.callbacks.forEach((fn) => fn(value));
                }
            }

            //Promise应用
            let p = new Promise((resolve) => {
                console.log('cb...');
                setTimeout(() => {
                    console.log('done');
                    resolve('1秒');
                }, 1000);
            })
            // .then((tip) => {
            //     console.log(tip);
            // });
        </script>
    </body>
</html>
